package com.heima.article.listen;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WemediaConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @KafkaListener(topics = WemediaConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        log.info("article-service 监听到消息：{}",message);
        if (!StringUtils.isBlank( message)) {
            WmNews wmNews = JSON.parseObject(message, WmNews.class);
            //0 下架  1 上架
            boolean is_down = false;
            if (wmNews.getEnable() == 0) {
                is_down = true;
            }
            apArticleConfigMapper.update(null, Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown,is_down)
                    .eq(ApArticleConfig::getArticleId,wmNews.getArticleId()));
            log.info("文章下架成功！{}",is_down);
        }


    }
}
